<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of myTools
 *
 * @author Tekla
 */
class myTools 
{
    
    /**
     *
     * @param type $rozgrywka_id
     * @param type $game_id
     * @param type $user_id
     * @return type 
     */
    public static function getStartDateByRozgrywkaId($rozgrywka_id, $game_id, $user_id)
    {
    $game_results = Doctrine_Core::getTable('GameResult')
      ->createQuery('a')
      ->select("a.*, DAYNAME(a.end_date) as dzien_tyg_en")
      ->where('a.user_id = ?', $user_id)
      ->addWhere('a.games_id = ?', $game_id)
      ->addWhere('a.rozgrywka_id = ?', $rozgrywka_id)
      ->orderBy('a.end_date DESC')
      ->groupBy('a.rozgrywka_id')
      ->execute();    
    
    foreach ($game_results as $game_result):
        $result = array('dzien' => $game_result->dzien_tyg_en, 'start' => $game_result->start_date);
    endforeach;
    
    return ($result);
    }
            
    public static function isUserHaveThatPermission($user_id, $permissionName)
    {
    
    $permissionsList = $this->getUser()->getPermissions(); 
    
    foreach ($permissionsList as $permission): 
        if($permission == $permissionName) {
            return (true);
        } else {
            return (false);
        }                        
    endforeach;
    }
    
    /**
     *
     * @param type $user_id
     * @return type 
     */
    public static function getUserById($user_id)
    {
    
    return Doctrine_Query::create()
      ->from('sfGuardUser r')
      ->where('r.id = ?', $user_id)
      ->limit(1)
      ->fetchOne();
    
    }
    
    public static function getUsersNumberByGroupId($group_id)
    {
    
    return Doctrine_Query::create()
      ->from('sfSocialGroupUser r')
      ->select('COUNT(r.user_id) as ilosc')
      ->where('r.group_id = ?', $group_id)
      ->execute();
    
    }
    
    public static function getJoinedUsersToGroup($group_id)
    {
    
    return Doctrine_Query::create()
      ->from('sfSocialGroupUser r')
      ->select('COUNT(r.user_id) as ilosc')
      ->where('r.group_id = ?', $group_id)
      ->execute();
    
    }
    
}

?>
